import 'package:get/get.dart';
import '../model/like_model.dart';
import '../data/like_data.dart';

/// 点赞状态管理
/// 使用GetX进行全局状态管理，采用非常规命名避免标准模式
class LikeState extends GetxController {
  // 所有点赞记录
  final RxList<LikeModel> _allLikes = <LikeModel>[].obs;

  // 当前用户ID
  final RxString _currentUserId = ''.obs;

  /// 获取所有点赞记录
  List<LikeModel> get allLikes => _allLikes.toList();

  /// 获取当前用户ID
  String get currentUserId => _currentUserId.value;

  /// 设置当前用户ID
  void setCurrentUserId(String userId) {
    _currentUserId.value = userId;
  }

  /// 点赞帖子
  void likePost(String postId) {
    // 检查是否已经点赞
    if (isPostLiked(postId)) {
      return;
    }

    final likeId = DateTime.now().millisecondsSinceEpoch.toString();
    final like = LikeModel(
      id: likeId,
      userId: _currentUserId.value,
      postId: postId,
      likeTime: DateTime.now(),
    );

    _allLikes.add(like);
  }

  /// 取消点赞帖子
  void unlikePost(String postId) {
    _allLikes.removeWhere(
      (like) => like.userId == _currentUserId.value && like.postId == postId,
    );
  }

  /// 切换帖子点赞状态
  void togglePostLike(String postId) {
    if (isPostLiked(postId)) {
      unlikePost(postId);
    } else {
      likePost(postId);
    }
  }

  /// 检查是否已点赞帖子
  bool isPostLiked(String postId) {
    return _allLikes.any(
      (like) => like.userId == _currentUserId.value && like.postId == postId,
    );
  }

  /// 获取帖子的点赞数量
  int getPostLikeCount(String postId) {
    return _allLikes.where((like) => like.postId == postId).length;
  }

  /// 获取用户点赞的帖子列表
  List<String> getLikedPostIds() {
    return _allLikes
        .where((like) => like.userId == _currentUserId.value)
        .map((like) => like.postId)
        .toList();
  }

  /// 获取帖子的点赞用户列表
  List<String> getPostLikedUserIds(String postId) {
    return _allLikes
        .where((like) => like.postId == postId)
        .map((like) => like.userId)
        .toList();
  }

  /// 获取用户的总点赞数
  int getUserTotalLikes(String userId) {
    return _allLikes.where((like) => like.userId == userId).length;
  }

  /// 删除帖子的所有点赞
  void removeAllLikesForPost(String postId) {
    _allLikes.removeWhere((like) => like.postId == postId);
  }

  /// 删除用户的所有点赞
  void removeAllLikesForUser(String userId) {
    _allLikes.removeWhere((like) => like.userId == userId);
  }

  /// 清空所有点赞记录
  void clearAllLikes() {
    _allLikes.clear();
  }

  /// 根据ID获取点赞记录
  LikeModel? getLikeById(String likeId) {
    try {
      return _allLikes.firstWhere((like) => like.id == likeId);
    } catch (e) {
      return null;
    }
  }

  /// 获取点赞统计信息
  Map<String, int> getLikeStatistics() {
    final stats = <String, int>{};

    // 总点赞数
    stats['totalLikes'] = _allLikes.length;

    // 当前用户点赞数
    stats['currentUserLikes'] =
        _allLikes.where((like) => like.userId == _currentUserId.value).length;

    return stats;
  }

  @override
  void onInit() {
    super.onInit();
    _loadDefaultData();
  }

  /// 加载默认数据
  void _loadDefaultData() {
    _allLikes.assignAll(LikeData.getDefaultLikes());
  }
}
